﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.IO;
using TierApplication;

namespace OrderMgr.OrderInfo
{
    public class TOrderFunc
    {
        public static TdsOrder GetOrderPrintList(string sOrderID)
        {
            TdsOrder dsOrder = new TdsOrder();
            //UserInfo
            T_Order mOrder = new T_Order();
            DataTable dtOrder = mOrder.GetOrderList(" and a.OrderID='" + sOrderID + "'").Tables[0];
            DataRow drOrder = dsOrder.TOrder.Rows.Add();
            drOrder["OrderID"] = dtOrder.Rows[0]["OrderID"].ToString();
            drOrder["OrderDate"] = DateTime.Parse(dtOrder.Rows[0]["OrderDate"].ToString()).ToString("yyyy-MM-dd");
            drOrder["OrderUserIDName"] = dtOrder.Rows[0]["OrderUserIDName"].ToString();
            drOrder["OrderActQuantity"] = decimal.Parse(dtOrder.Rows[0]["OrderActQuantity"].ToString()).ToString("0");
            drOrder["OrderActMoney"] = decimal.Parse(dtOrder.Rows[0]["OrderActMoney"].ToString()).ToString("0.00");
            drOrder["DeportIDName"] = dtOrder.Rows[0]["DeportIDName"].ToString();
            drOrder["ToDepotIDName"] = dtOrder.Rows[0]["ToDepotIDName"].ToString();
            drOrder["AuditUserIDName"] = dtOrder.Rows[0]["AuditUserIDName"].ToString();
            drOrder["POrderID"] = dtOrder.Rows[0]["POrderID"].ToString();
            drOrder["ContactAddress"] = dtOrder.Rows[0]["ContactAddress"].ToString();
            drOrder["Mobile"] = dtOrder.Rows[0]["Mobile"].ToString();
            drOrder["CustName"] = dtOrder.Rows[0]["CustName"].ToString();
            drOrder["Remark"] = dtOrder.Rows[0]["Remark"].ToString();
            drOrder["ContactName"] = dtOrder.Rows[0]["ContactName"].ToString();

            T_OrderDetail mOrderDetail = new T_OrderDetail();
            DataTable dtOrderDetail = mOrderDetail.GetOrderDetailList(" and OrderID='" + sOrderID + "'").Tables[0];
            for (int i = 0; i < dtOrderDetail.Rows.Count; i++)
            {
                DataRow drOrderDetail = dsOrder.TOrderDetailEx.Rows.Add();
                drOrderDetail["OrderID"] = dtOrderDetail.Rows[i]["OrderID"].ToString();
                drOrderDetail["ProductID"] = dtOrderDetail.Rows[i]["ProductID"].ToString();
                drOrderDetail["ProductNo"] = dtOrderDetail.Rows[i]["ProductNo"].ToString();
                drOrderDetail["BatchNo"] = dtOrderDetail.Rows[i]["BatchNo"].ToString();
                drOrderDetail["UsefulLifeName"] = dtOrderDetail.Rows[i]["UsefulLifeName"].ToString();
                drOrderDetail["DepotArea"] = dtOrderDetail.Rows[i]["DepotArea"].ToString();
                drOrderDetail["PlnQuantity"] = decimal.Parse(dtOrderDetail.Rows[i]["PlnQuantity"].ToString()).ToString("0");
                drOrderDetail["ShpQuantity"] = decimal.Parse(dtOrderDetail.Rows[i]["ShpQuantity"].ToString()).ToString("0");
                drOrderDetail["ActQuantity"] = decimal.Parse(dtOrderDetail.Rows[i]["ActQuantity"].ToString()).ToString("0");
                drOrderDetail["BckQuantity"] = decimal.Parse(dtOrderDetail.Rows[i]["BckQuantity"].ToString()).ToString("0");
                drOrderDetail["UnitPrice"] = decimal.Parse(dtOrderDetail.Rows[i]["UnitPrice"].ToString()).ToString("0.00");
                drOrderDetail["SumPrice"] =decimal.Parse(dtOrderDetail.Rows[i]["SumPrice"].ToString()).ToString("0.00");
                drOrderDetail["UnitName"] = dtOrderDetail.Rows[i]["UnitName"].ToString();
                drOrderDetail["SupplierName"] = dtOrderDetail.Rows[i]["SupplierName"].ToString();
                drOrderDetail["Spec1"] = dtOrderDetail.Rows[i]["Spec1"].ToString();
                drOrderDetail["ProductName"] = dtOrderDetail.Rows[i]["ProductName"].ToString();
                drOrderDetail["BaseProductPlace"] = dtOrderDetail.Rows[i]["BaseProductPlace"].ToString();
                drOrderDetail["ProductTypeName"] = dtOrderDetail.Rows[i]["ProductTypeName"].ToString();
                drOrderDetail["DepotArea"] = dtOrderDetail.Rows[i]["DepotArea"].ToString();
                
            }
            return dsOrder;
        }

        public static TdsOrder GetOrderList(string swhere)
        {
            TdsOrder dsOrder = new TdsOrder();
            //UserInfo
            T_Order mOrder = new T_Order();
            DataTable dtOrder = mOrder.GetOrderList(swhere).Tables[0];
            for (int i = 0; i < dtOrder.Rows.Count; i++)
            {
                DataRow drOrder = dsOrder.TOrder.Rows.Add();
                drOrder["OrderID"] = dtOrder.Rows[i]["OrderID"].ToString();
                drOrder["OrderDate"] = DateTime.Parse(dtOrder.Rows[i]["OrderDate"].ToString()).ToString("yyyy-MM-dd");
                drOrder["OrderUserIDName"] = dtOrder.Rows[i]["OrderUserIDName"].ToString();
                drOrder["CompositeProductName"] = dtOrder.Rows[i]["CompositeProductName"].ToString();
                drOrder["CompositeNum"] = decimal.Parse(dtOrder.Rows[i]["CompositeNum"].ToString()).ToString("0.00");
                drOrder["CompositePrice"] = dtOrder.Rows[i]["CompositePrice"].ToString();
            }

            return dsOrder;
        }



        public static int PintOrderShip(string sOrderID, string sShipCompany, Boolean print)
        {

            string sFileName = Application.StartupPath + "\\物流单\\" + sShipCompany + ".repx";
            if (!System.IO.File.Exists(sFileName))
            {
                MessageBox.Show(sShipCompany + "快递模板不存在,不能打印此快递公司单!");
                return -100;
            }

            DataTable dtOrderShipSetup = T_OrderShip.GetOrderShipSetup().Tables[0];
            T_Order mOrder = new T_Order();
            DataTable dtOrderCust = mOrder.GetOrderList(" and a.OrderID='" + sOrderID + "'").Tables[0];

            DevExpress.XtraReports.UI.XtraReport fReport = new DevExpress.XtraReports.UI.XtraReport();
            //string sFileName = Application.StartupPath + "\\物流单\\" + sShipCompany + ".repx";
            try
            {
                fReport.LoadLayout(sFileName);
            }
            catch (Exception e)
            {
                MessageBox.Show("加载打印模板出错,请确认打印模板正确!");
                return -200;
            }

            fReport.Parameters["发件人姓名"].Value = dtOrderShipSetup.Rows[0]["发件人姓名"].ToString();
            fReport.Parameters["发件人单位"].Value = dtOrderShipSetup.Rows[0]["发件人单位"].ToString();
            fReport.Parameters["发件人省份"].Value = dtOrderShipSetup.Rows[0]["发件人省份"].ToString();
            fReport.Parameters["发件人城市"].Value = dtOrderShipSetup.Rows[0]["发件人城市"].ToString();
            fReport.Parameters["发件人地址"].Value = dtOrderShipSetup.Rows[0]["发件人地址"].ToString();
            fReport.Parameters["发件人手机"].Value = dtOrderShipSetup.Rows[0]["发件人手机"].ToString();
            fReport.Parameters["发件人电话"].Value = dtOrderShipSetup.Rows[0]["发件人电话"].ToString();
            fReport.Parameters["收件人姓名"].Value = dtOrderCust.Rows[0]["ContactName"].ToString();
            fReport.Parameters["收件人单位"].Value = dtOrderCust.Rows[0]["CustName"].ToString();
            fReport.Parameters["收件人省份"].Value = dtOrderCust.Rows[0]["Region"].ToString();
            fReport.Parameters["收件人城市"].Value = dtOrderCust.Rows[0]["CityName"].ToString();
            fReport.Parameters["收件人地址"].Value = dtOrderCust.Rows[0]["ContactAddress"].ToString();
            fReport.Parameters["收件人手机"].Value = dtOrderCust.Rows[0]["Mobile"].ToString();
            fReport.Parameters["收件人电话"].Value = dtOrderCust.Rows[0]["Phone"].ToString();
            fReport.RequestParameters = false;
            if (print)
            {
                fReport.Print();
            }
            else
            {
                fReport.ShowPreview();
            }
            return 0;
        }

        public static int PintOrderShip(string sOrderID, string sShipCompany)
        {
            return PintOrderShip(sOrderID, sShipCompany, true);
        }
        public static int PintOrderShipPreview(string sOrderID, string sShipCompany)
        {
            return PintOrderShip(sOrderID, sShipCompany, false);
        }
    }
}
